package now_coder.stack.ncs;

import java.util.Stack;

public class NC52 {

    public boolean isValid (String s) {
        if (s == null || s.length() == 0)
            return false;

        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!stack.isEmpty()){
                boolean flag = false;
                if (ch == ']' && stack.peek() == '[')
                    flag = true;
                else if (ch == '}' && stack.peek() == '{')
                    flag = true;
                else if (ch == ')' && stack.peek() == '(')
                    flag = true;

                if (flag) {
                    stack.pop();
                    continue;
                }
            }

            stack.push(ch);
        }

        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(new NC52().isValid("(([{]}){})"));
    }

}
